package cn.schoolwow.ams.module.block.listBlock.daoBlock.databaseTable.initial.service;

import cn.schoolwow.ams.domain.block.list.common.ListBlockOption;
import cn.schoolwow.ams.domain.block.list.dao.databaseTable.DatabaseTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.databaseTable.DatabaseTableListBlockOption;
import cn.schoolwow.ams.domain.option.QuickAMSListener;
import cn.schoolwow.ams.module.block.listBlock.common.domain.context.ListBlockContext;
import cn.schoolwow.ams.module.block.listBlock.daoBlock.common.domain.DatabaseListBlockContext;
import cn.schoolwow.ams.module.block.listBlock.daoBlock.databaseTable.initial.domain.DatabaseTableListBlockContext;
import cn.schoolwow.ams.module.block.listBlock.daoBlock.databaseTable.initial.flow.InitialDatabaseTableListBlockContextFlow;
import cn.schoolwow.ams.module.initial.domain.InitialAMSContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import org.apache.commons.lang3.Validate;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class InitialDatabaseTableListBlockListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setDatabaseTableListBlockOptionList(flowContext);
        setDatabaseTableListBlockContextList(flowContext);
    }

    @Override
    public String name() {
        return "初始化DatabaseTableListBlock类型页面";
    }

    private void setDatabaseTableListBlockOptionList(FlowContext flowContext){
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        Map<String,DatabaseTableListBlockConfigAware> map = applicationContext.getBeansOfType(DatabaseTableListBlockConfigAware.class);

        if (map.isEmpty()) {
            flowContext.brokenCurrentFlow("无DatabaseTableListBlock配置类列表信息");
        }
        List<DatabaseTableListBlockOption> databaseTableListBlockOptionList = new ArrayList<>();
        for(DatabaseTableListBlockConfigAware databaseTableListBlockConfigAware:map.values()){
            DatabaseTableListBlockOption databaseTableListBlockOption = databaseTableListBlockConfigAware.getAMSBlockConfig();
            databaseTableListBlockOption.routerBlock = databaseTableListBlockConfigAware;

            String className = databaseTableListBlockConfigAware.getClass().getName();
            CheckInstance.newCheck()
                    .notNullCheck(databaseTableListBlockOption, "clazz表配置,涉及类名:["+className+"]")
                    .notEmptyCheck(databaseTableListBlockOption.projectName, "项目名称,涉及类名:["+className+"]")
                    .notEmptyCheck(databaseTableListBlockOption.displayName, "表显示名称,涉及类名:["+className+"]")
                    .notNullCheck(databaseTableListBlockOption.daoName, "数据库名称,涉及类名:["+className+"]")
                    .notNullCheck(databaseTableListBlockOption.tableName, "表名,涉及类名:["+className+"]");
            databaseTableListBlockOptionList.add(databaseTableListBlockOption);
        }
        flowContext.putContextData("databaseTableListBlockOptionList", databaseTableListBlockOptionList);
    }

    private void setDatabaseTableListBlockContextList(FlowContext flowContext){
        List<DatabaseTableListBlockOption> databaseTableListBlockOptionList = (List<DatabaseTableListBlockOption>) flowContext.checkData("databaseTableListBlockOptionList");
        InitialAMSContext initialAMSContext = flowContext.checkInstanceData(InitialAMSContext.class);
        List<QuickAMSListener> quickAMSListenerList = (List<QuickAMSListener>) flowContext.checkData("quickAMSListenerList");

        List<DatabaseTableListBlockContext> databaseTableListBlockContextList = new ArrayList<>();
        for(DatabaseTableListBlockOption databaseTableListBlockOption:databaseTableListBlockOptionList){
            DatabaseTableListBlockContext databaseTableListBlockContext = new DatabaseTableListBlockContext();
            databaseTableListBlockContext.projectName = databaseTableListBlockOption.projectName;
            databaseTableListBlockContext.displayName = databaseTableListBlockOption.displayName;
            databaseTableListBlockContext.uniqueKey = databaseTableListBlockOption.routerBlock.getBlockUniqueName();
            for(QuickAMSListener quickAMSListener:quickAMSListenerList){
                databaseTableListBlockContext.dao = quickAMSListener.getDAO(databaseTableListBlockOption.daoName);
                if(null!=databaseTableListBlockContext.dao){
                    break;
                }
            }
            Validate.notNull(databaseTableListBlockContext.dao, "数据库不存在!数据库名:"+databaseTableListBlockOption.daoName);
            databaseTableListBlockContext.entity = databaseTableListBlockContext.dao.getDatabaseEntity(databaseTableListBlockOption.tableName);
            Validate.notNull(databaseTableListBlockContext.entity, "数据库实体类不存在!表名:"+databaseTableListBlockOption.tableName);

            flowContext.startFlow(new InitialDatabaseTableListBlockContextFlow())
                    .putTemporaryInstanceData(databaseTableListBlockOption)
                    .putTemporaryInstanceData(databaseTableListBlockContext)
                    .putTemporaryInstanceData(databaseTableListBlockOption, ListBlockOption.class)
                    .putTemporaryInstanceData(databaseTableListBlockContext, ListBlockContext.class)
                    .putTemporaryInstanceData(databaseTableListBlockContext, DatabaseListBlockContext.class)
                    .execute();

            databaseTableListBlockContextList.add(databaseTableListBlockContext);
            initialAMSContext.listBlockContextList.add(databaseTableListBlockContext);
        }
        flowContext.putTemporaryData("databaseTableListBlockContextList", databaseTableListBlockContextList);
    }
}
